package com.superteam.zzl.adapter.SearchAdapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.model.Search.DynData;
import com.superteam.zzl.model.Search.GropData;
import com.superteam.zzl.model.Search.PoiData;
import com.superteam.zzl.model.Search.SearchData;
import com.superteam.zzl.model.Search.ShopData;
import com.superteam.zzl.model.Search.UserData;
import com.superteam.zzl.utils.TransfromTime;
import com.superteam.zzl.viewmodel.Search.Data;

import java.util.ArrayList;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


/**
 * Created by zhang on 2017/1/13.
 *
 * //    private ArrayList<SearchData.PlaceListBean> list;//查找的结果
 //    private ArrayList<SearchData.TrendListBean> trendList;//动态
 //    private ArrayList<SearchData.ProductListBean> productList; //商品
 //    private ArrayList<SearchData.PoiListBean> poiList;//旅行地
 //    private ArrayList<SearchData.UserListBean> userList;//用户
 //    private ArrayList<SearchData.GroupListBean> groupList;//群组
 */

public class SearchResultAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Object> list;
    private Object object;
    private Data data;
    private DynData dynData;
    private ShopData shopData;
    private PoiData poiData;
    private UserData userData;
    private int flag;


    public SearchResultAdapter(Context context, ArrayList<Object> list) {
        this.context = context;
        this.list = list;
        data = new Data();
        dynData = new DynData();
        shopData = new ShopData();
        poiData = new PoiData();
        userData = new UserData();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        object = list.get(position);
        //Log.e("data",object.toString());
        //结果
        if(object instanceof SearchData.PlaceListBean) {
            for (int i = 0; i < list.size(); i++) {
                data.placeList.add((SearchData.PlaceListBean) list.get(i));
            }
            return 1;

        }else if(object instanceof SearchData.TrendListBean || object instanceof DynData.TrendListBean){//动态
            if(object instanceof SearchData.TrendListBean) {
                for (int i = 0; i < list.size(); i++) {
                    data.trendList.add((SearchData.TrendListBean) list.get(i));
                }
            }else{
                for (int i = 0; i < list.size(); i++) {
                    dynData.trendLists.add((DynData.TrendListBean) list.get(i));
                }
            }
            return 2;
        }else if(object instanceof SearchData.ProductListBean || object instanceof ShopData.ProductListBean){//商品
            if(object instanceof SearchData.ProductListBean ){
                for (int i = 0; i < list.size(); i++) {
                    data.productList.add((SearchData.ProductListBean) list.get(i));
                }
            }else{
                for (int i = 0; i < list.size(); i++) {
                    Log.e("data",list.size()+"");
                    Log.e("data",shopData.productLists.size()+"");
                    shopData.productLists.add((ShopData.ProductListBean) list.get(i));
                }
            }
            return 3;
        }else if(object instanceof SearchData.PoiListBean || object instanceof PoiData.PoiListBean){//旅行地
            if(object instanceof SearchData.PoiListBean) {
                for (int i = 0; i < list.size(); i++) {
                    data.poiList.add((SearchData.PoiListBean) list.get(i));
                }
            }else{
                for (int i = 0; i < list.size(); i++) {
                    poiData.poiLists.add((PoiData.PoiListBean) list.get(i));
                }
            }
            return 4;
        }else if(object instanceof SearchData.UserListBean || object instanceof UserData.UserListBean){//用户
            if(object instanceof SearchData.UserListBean) {
                for (int i = 0; i < list.size(); i++) {
                    data.userList.add((SearchData.UserListBean) list.get(i));
                }
            }else{
                for (int i = 0; i < list.size(); i++) {
                    userData.userLists.add((UserData.UserListBean) list.get(i));
                }
            }
            flag = 1;
            return 5;
        }
        else if(object instanceof SearchData.GroupListBean || object instanceof GropData.GroupListBean){//群组
            for (int i = 0; i < list.size(); i++) {
                data.groupList.add((SearchData.GroupListBean) list.get(i));
            }
            flag = 2;
            return 5;
        }
        else{
                return -1;
            }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 vh1;
        ViewHolder2 vh2;
        ViewHolder3 vh3;
        ViewHolder4 vh4;
        ViewHolder5 vh5;
        switch (getItemViewType(position)){
            case 1:
                if(convertView == null){
                    convertView = View.inflate(context, R.layout.resultlist,null);
                    vh1 = new ViewHolder1();
                    vh1.iv = (ImageView) convertView.findViewById(R.id.iv_des);
                    vh1.tv_title = (TextView)convertView.findViewById(R.id.tv_adress);
                    vh1.title_en = (TextView)convertView.findViewById(R.id.tv_adress_1);
                    convertView.setTag(vh1);
                }else{
                    vh1 = (ViewHolder1) convertView.getTag();
                }
                vh1.tv_title.setText(data.placeList.get(position).getTitle());
                vh1.title_en.setText(data.placeList.get(position).getTitle_en());
                if(Util.isOnMainThread()) {
                    Glide.with(context.getApplicationContext()).load(data.placeList.get(position).getCoverImage()).into(vh1.iv);
                }
                break;
            case 2:
                if(convertView == null){
                    convertView = View.inflate(context,R.layout.result_lv_dyn,null);
                    vh2 = new ViewHolder2();
                    vh2.iv = (ImageView) convertView.findViewById(R.id.iv_res_lv_dyn);
                    vh2.tv_name = (TextView)convertView.findViewById(R.id.tv_res_lv_dyn_name);
                    vh2.tv_adress = (TextView)convertView.findViewById(R.id.tv_res_lv_dyn_adress);
                    vh2.tv_time = (TextView)convertView.findViewById(R.id.tv_time);
                    vh2.tv_content = (TextView)convertView.findViewById(R.id.tv_res_lv_dyn_content);
                    convertView.setTag(vh2);
                }else{
                    vh2 = (ViewHolder2) convertView.getTag();
                }
                if(object instanceof SearchData.TrendListBean) {
                    vh2.tv_name.setText(data.trendList.get(position).getUser_name());
                    if (data.trendList.get(position).getPlace_name() != null) {
                        vh2.tv_adress.setText(" | " + data.trendList.get(position).getPlace_name());
                    } else {
                        vh2.tv_adress.setText("");
                    }
                    vh2.tv_time.setText(TransfromTime.getTime(Integer.parseInt(data.trendList.get(position).getCtime())));
                    if (data.trendList.get(position).getTags() == null) {
                        vh2.tv_content.setText(data.trendList.get(position).getContent());
                    } else {
                        StringBuilder tag = new StringBuilder();
                        for (int i = 0; i < data.trendList.get(position).getTags().size(); i++) {
                            tag.append(data.trendList.get(position).getTags().get(i).getTag_name() + " ");
                        }

                        vh2.tv_content.setText(tag + " " + data.trendList.get(position).getContent());
                    }
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(data.trendList.get(position).getUser_avatar()).bitmapTransform(new CropCircleTransformation(Glide.get(context.getApplicationContext()).getBitmapPool())).into(vh2.iv);
                    }
                }else{
                    vh2.tv_name.setText(dynData.trendLists.get(position).getUser_name());
                    if (dynData.trendLists.get(position).getPlace_name() != null) {
                        vh2.tv_adress.setText(" | " + dynData.trendLists.get(position).getPlace_name());
                    } else {
                        vh2.tv_adress.setText("");
                    }
                    vh2.tv_time.setText(TransfromTime.getTime(Integer.parseInt(dynData.trendLists.get(position).getCtime())));
                    if (dynData.trendLists.get(position).getTags() == null) {
                        vh2.tv_content.setText(dynData.trendLists.get(position).getContent());
                    } else {
                        StringBuilder tag = new StringBuilder();
                        for (int i = 0; i < dynData.trendLists.get(position).getTags().size(); i++) {
                            tag.append(dynData.trendLists.get(position).getTags().get(i).getTag_name() + " ");
                        }

                        vh2.tv_content.setText(tag + " " + dynData.trendLists.get(position).getContent());
                    }
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(dynData.trendLists.get(position).getUser_avatar()).bitmapTransform(new CropCircleTransformation(Glide.get(context.getApplicationContext()).getBitmapPool())).into(vh2.iv);
                    }
                }
                break;
            case 3:
                if(convertView == null){
                    convertView = View.inflate(context,R.layout.result_lv_shop,null);
                    vh3 = new ViewHolder3();
                    vh3.tv_title = (TextView) convertView.findViewById(R.id.tv_res_lv_shop_title);
                    vh3.tv_sales = (TextView) convertView.findViewById(R.id.tv_res_lv_shop_sale);
                    vh3.tv_evalute = (TextView) convertView.findViewById(R.id.tv_res_lv_shop_pingjia);
                    vh3.tv_price = (TextView) convertView.findViewById(R.id.tv_res_lv_shop_price);
                    vh3.iv = (ImageView)convertView.findViewById(R.id.iv_res_lv_shop);
                    convertView.setTag(vh3);
                }else{
                    vh3 = (ViewHolder3) convertView.getTag();
                }
                if(object instanceof SearchData.ProductListBean) {
                    vh3.tv_title.setText(data.productList.get(position).getTitle());
                    if (data.productList.get(position).getSales_count() != null) {
                        vh3.tv_sales.setText("销量" + data.productList.get(position).getSales_count());
                    } else {
                        vh3.tv_sales.setText("");
                    }
                    if (data.productList.get(position).getEvaluate_count() != null) {
                        vh3.tv_evalute.setText(" | 评价" + data.productList.get(position).getEvaluate_count());
                    } else {
                        vh3.tv_evalute.setText("");
                    }
                    String price = data.productList.get(position).getLprice();
                    if(price.length()>2) {
                        vh3.tv_price.setText("¥" + price.substring(0, price.length() - 2));
                    }else{
                        vh3.tv_price.setText("¥" + price);
                    }
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(data.productList.get(position).getCoverImage()).into(vh3.iv);
                    }
                }else{
                    vh3.tv_title.setText(shopData.productLists.get(position).getTitle());
                    if (shopData.productLists.get(position).getSales_count() != null) {
                        vh3.tv_sales.setText("销量" + shopData.productLists.get(position).getSales_count());
                    } else {
                        vh3.tv_sales.setText("");
                    }
                    if (shopData.productLists.get(position).getEvaluate_count() != null) {
                        vh3.tv_evalute.setText(" | 评价" + shopData.productLists.get(position).getEvaluate_count());
                    } else {
                        vh3.tv_evalute.setText("");
                    }
                    String price = shopData.productLists.get(position).getLprice();
                    vh3.tv_price.setText("¥" + price.substring(0, price.length() - 2));
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(shopData.productLists.get(position).getCoverImage()).into(vh3.iv);
                    }
                }
                break;
            case 4:
                if(convertView == null){
                    convertView = View.inflate(context,R.layout.result_lv_tra,null);
                    vh4 = new ViewHolder4();
                    vh4.iv = (ImageView) convertView.findViewById(R.id.iv_res_lv_tra);
                    vh4.tv_title = (TextView)convertView.findViewById(R.id.tv_res_lv_tra_itle);
                    vh4.tv_type = (TextView)convertView.findViewById(R.id.tv_res_lv_tra_type);
                    convertView.setTag(vh4);
                }else{
                    vh4 = (ViewHolder4) convertView.getTag();
                }
                if(object instanceof SearchData.PoiListBean) {
                    vh4.tv_title.setText(data.poiList.get(position).getTitle());
                    if (data.poiList.get(position).getType() != null) {
                        if (data.poiList.get(position).getType().equals("restaurant")) {
                            vh4.tv_type.setText("[美食]");
                        } else if (data.poiList.get(position).getType().equals("shopping")) {
                            vh4.tv_type.setText("[购物]");
                        } else {
                            vh4.tv_type.setText("[" + data.poiList.get(position).getType() + "]");
                        }
                    }
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(data.poiList.get(position).getCoverImage()).into(vh4.iv);
                    }
                }else{
                    vh4.tv_title.setText(poiData.poiLists.get(position).getTitle());
                    if (poiData.poiLists.get(position).getType() != null) {
                        if (poiData.poiLists.get(position).getType().equals("restaurant")) {
                            vh4.tv_type.setText("[美食]");
                        } else if (poiData.poiLists.get(position).getType().equals("shopping")) {
                            vh4.tv_type.setText("[购物]");
                        } else {
                            vh4.tv_type.setText("[" + poiData.poiLists.get(position).getType() + "]");
                        }
                    }
                    if (Util.isOnMainThread()) {
                        Glide.with(context.getApplicationContext()).load(poiData.poiLists.get(position).getCoverImage()).into(vh4.iv);
                    }
                }
                break;
            case 5:
                if(convertView == null){
                    convertView = View.inflate(context,R.layout.res_user,null);
                    vh5 = new ViewHolder5();
                    vh5.iv = (ImageView)convertView.findViewById(R.id.iv_res_lv_user);
                    vh5.tv_name = (TextView)convertView.findViewById(R.id.tv_res_lv_user_title);
                    vh5.tv_id = (TextView)convertView.findViewById(R.id.tv_res_lv_user_id);
                    convertView.setTag(vh5);
                }else{
                    vh5 = (ViewHolder5) convertView.getTag();
                }
                if(flag == 1){
                    if(object instanceof SearchData.UserListBean) {
                        vh5.tv_id.setText("ID:" + data.userList.get(position).getUser_id());
                        vh5.tv_name.setText(data.userList.get(position).getTitle());
                        if (Util.isOnMainThread()) {
                            Glide.with(context.getApplicationContext()).load(data.userList.get(position).getCoverImage()).bitmapTransform(new CropCircleTransformation(Glide.get(context.getApplicationContext()).getBitmapPool())).into(vh5.iv);
                        }
                    }
                    else{
                        vh5.tv_id.setText("ID:" + userData.userLists.get(position).getUser_id());
                        vh5.tv_name.setText(userData.userLists.get(position).getTitle());
                        if (Util.isOnMainThread()) {
                            Glide.with(context.getApplicationContext()).load(userData.userLists.get(position).getCoverImage()).bitmapTransform(new CropCircleTransformation(Glide.get(context.getApplicationContext()).getBitmapPool())).into(vh5.iv);
                        }
                    }
                }else if(flag == 2){
                    vh5.tv_id.setText(data.groupList.get(position).getGroup_id());
                    vh5.tv_name.setText(data.groupList.get(position).getTitle());
                    if(Util.isOnMainThread()){
                        Glide.with(context.getApplicationContext()).load(data.groupList.get(position).getCoverImage()).bitmapTransform(new CropCircleTransformation(Glide.get(context.getApplicationContext()).getBitmapPool())).into(vh5.iv);
                    }
                }
                break;
        }

        return convertView;
    }

    //所搜结果
    class ViewHolder1{
        ImageView iv;
        TextView tv_title,title_en;
    }
    //动态
    class ViewHolder2{
        ImageView iv;
        TextView tv_name,tv_adress,tv_time,tv_content;

    }
    //商品
    class ViewHolder3{
        ImageView iv;
        TextView tv_title,tv_sales,tv_evalute,tv_price;
    }
    //旅行地
    class ViewHolder4{
        ImageView iv;
        TextView tv_title,tv_type;
    }
    //用户  群组
    class ViewHolder5{
        ImageView iv;
        TextView tv_name,tv_id;
    }


}
